//
//  XJNewFeatureViewController.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/22.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJNewFeatureViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
   
        view.addSubview(collectionView)
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
    }
    
    private lazy var collectionView: UICollectionView = {
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
        
        flowLayout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
       
        let coll = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        coll.backgroundColor =  RandomColor()
        coll.pagingEnabled = true
        coll.bounces = false
        coll.registerClass(XJNewfeatureCell.self, forCellWithReuseIdentifier: "cell")
        coll.showsHorizontalScrollIndicator = false
        coll.showsVerticalScrollIndicator = false
        coll.dataSource = self
        coll.delegate = self
        
        return coll
        
    }()

}

extension XJNewFeatureViewController: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! XJNewfeatureCell
      cell.imgIndex = indexPath.item
        
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        if index == 3 {
            
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! XJNewfeatureCell
            
            cell.startAnim()
        }
    }
}

class XJNewfeatureCell: UICollectionViewCell {
    var imgIndex :Int = 0 {
        didSet{
            bgImageView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            button.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI() {
        backgroundColor = RandomColor()
    contentView.addSubview(bgImageView)
    contentView.addSubview(button)
    
    bgImageView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
    }
    button.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(contentView)
        make.bottom.equalTo(contentView).offset(-100)
    }
    button.hidden = true
    }
    //设置按钮动画
    func startAnim() {
        button.hidden = false
        self.button.transform = CGAffineTransformMakeScale(0, 0)
        self.button.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.button.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.button.userInteractionEnabled = true
        }
    }
    
    
    
    //懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView()
    
    private lazy var button: UIButton = {
       
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("欢迎观临", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.sizeToFit()
        button.addTarget(self, action: "didClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func didClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newfeature")
    }
    
}













